package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrType;

public class ReportNotificationRecordList  extends AxdrType {
    private byte[] dataCode = null;
    private PIID piid = null;
    private SubSeqOfResultRecord resultRecords = null;

    public byte[] getDataCode() {
        return dataCode;
    }

    public PIID getPiid() {
        return piid;
    }

    public SubSeqOfResultRecord getResultRecords() {
        return resultRecords;
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(PIID piid, SubSeqOfResultRecord resultRecords) {
        this.piid = piid;
        this.resultRecords = resultRecords;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.piid = new PIID();
        this.piid.setNumBit(8);
        codeLength += this.piid.decode(input);
        this.resultRecords = new SubSeqOfResultRecord();
        codeLength += this.resultRecords.decode(input);
        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength;
        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if (this.resultRecords != null && this.piid != null) {
                codeLength = 0;
                codeLength += this.resultRecords.encode(output);
                codeLength += this.piid.encode(output);
            } else {
                throw new RuntimeException("resultRecords or piid is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (this.piid != null && this.resultRecords != null) {
            return "sequence: {" + "piid: " + this.piid + ", resultRecords: " + this.resultRecords + "}";
        } else {
            return "piid || resultRecords is null";
        }
    }
}
